package com.itsci.assignment2;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class LoginManager extends SQLiteOpenHelper{

	public LoginManager(Context context, String name, CursorFactory factory,
			int version) {
		super(context, "Login", null, version);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL("create table Login(username text PRIMARY KEY,password text);");
		
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("drop table Login;");
	}
	
	public void insertLogin(LoginBean login){
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues content = new ContentValues();
		content.put("username",login.getUsername());
		content.put("password",login.getPassword());
		db.insert("Login", null, content);
		db.close();
	}
	public LoginBean getByStudentId(String id) {
		SQLiteDatabase db = this.getWritableDatabase();
		Cursor cursor = db.query("Login", new String[] { "username","password"},
				"username = ?", new String[] { id }, null, null, null);
		LoginBean login = new LoginBean();
		if (cursor.moveToFirst()) {
			login.setUsername(cursor.getString(0));
			login.setPassword(cursor.getString(1));
		} else {
			login.setUsername("");
			login.setPassword("");
		}
		db.close();
		return login;
	}

}
